<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use app\common\model\ViewGetStudentList as ViewGetStudentListModel;
use app\common\model\BookDraw as BookDrawModel;
use app\common\model\BookReturn as BookReturnModel;
use app\common\model\Config as ConfigModel;
use think\model\concern\SoftDelete;
use think\facade\Log;

class BookAccount extends Common
{
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }
       
        $tableWhere = $this->tableWhere($post);
        $list = $this->alias('a')
                        ->field("a.id, a.year, case when a.season = 1 then '春季' when a.season = 3 then '秋季' end season, d.bmmc, e.zymc, f.bjmc, c.xh, c.xm, h.book_name, h.book_author, h.book_concern, h.book_edition, h.book_isbn, b.book_num, b.book_price, b.book_disc, FROM_UNIXTIME(a.date) date, g.username")
                        ->join('zl_book_account_detail b', 'a.id = b.account_id')
                        ->leftjoin('zl_student_info c', 'a.xh = c.xh')
                        ->leftjoin('zlzhsf.pbmdm d', 'a.bmdm = d.bmdm')
                        ->leftjoin('zlzhsf.pzydm e', 'a.zydm = e.zydm')
                        ->leftjoin('zlzhsf.pbjdm f', 'a.bjdm = f.bjdm')
                        ->leftjoin('zl_manage g', 'a.operid = g.id')
                        ->leftjoin('zl_book_info h', 'b.book_id = h.id')
                        ->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;
        return $re;
    }
  
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getExportData($post)
    {
        $tableWhere = $this->tableWhere($post);
        $list = $this->alias('a')
                        ->field("a.id, a.year, case when a.season = 1 then '春季' when a.season = 3 then '秋季' end season, d.bmmc, e.zymc, f.bjmc, c.xh, c.xm, h.book_name, h.book_author, h.book_concern, h.book_edition, h.book_isbn, b.book_num, b.book_price, b.book_disc, FROM_UNIXTIME(a.date) date, g.username")
                        ->join('zl_book_account_detail b', 'a.id = b.account_id')
                        ->leftjoin('zl_student_info c', 'a.xh = c.xh')
                        ->leftjoin('zlzhsf.pbmdm d', 'a.bmdm = d.bmdm')
                        ->leftjoin('zlzhsf.pzydm e', 'a.zydm = e.zydm')
                        ->leftjoin('zlzhsf.pbjdm f', 'a.bjdm = f.bjdm')
                        ->leftjoin('zl_manage g', 'a.operid = g.id')
                        ->leftjoin('zl_book_info h', 'b.book_id = h.id')
                        ->where($tableWhere['where'])->order($tableWhere['order'])->select();
                        
        return $list;
    }
  
    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];
        if(isset($post['year']) && $post['year'] != ""){
            $where[] = ['a.year', '=', $post['year']];
        }
        if(isset($post['season']) && $post['season'] != ""){
            $where[] = ['a.season', '=', $post['season']];
        }
        if(isset($post['dep_id']) && $post['dep_id'] != ""){
            if($post['dep_id'] ==  "00203"){
                $where[] = ['a.bmdm', 'in', "00203,00403"];
            } else if($post['dep_id'] ==  "00204"){
                $where[] = ['a.bmdm', 'in', "00204,00404"];
            } else if($post['dep_id'] ==  "00205"){
                $where[] = ['a.bmdm', 'in', "00205,00405"];
            } else if($post['dep_id'] ==  "00210"){
                $where[] = ['a.bmdm', 'in', "00210,00410"];
            } else if($post['dep_id'] ==  "00214"){
                $where[] = ['a.bmdm', 'in', "00214,00413"];
            } else if($post['dep_id'] ==  "00216"){
                $where[] = ['a.bmdm', 'in', "00216,00414"];
            } else if($post['dep_id'] ==  "00217"){
                $where[] = ['a.bmdm', 'in', "00217,00415"];
            } else {
                $where[] = ['a.bmdm', '=', $post['dep_id']];
            }
        }
        if(isset($post['specialty_id']) && $post['specialty_id'] != ""){
            $where[] = ['a.zydm', '=', $post['specialty_id']];
        }
        if(isset($post['class_id']) && $post['class_id'] != ""){
            $where[] = ['a.bjdm', '=', $post['class_id']];
        }
        if(isset($post['xh']) && $post['xh'] != ""){
            $where[] = ['a.xh', '=', $post['xh']];
        }
        if(isset($post['xm']) && $post['xm'] != ""){
            $where[] = ['c.xm', 'like', '%'.urldecode($post['xm']).'%'];
        }
        if(isset($post['id']) && $post['id'] != ""){
            $where[] = ['a.id', 'eq', $post['id']];
        }
        
        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['a.id ASC'];
        return $result;
    }

    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }

    /**
     * 出账
     * @param $data
     * @return array
     */
    public function bookAccount($data)
    {
        $return = [
            'status' => false,
            'msg'    => '汇总账单失败',
            'data'   => ''
        ];

        // 判断是否存在教材没有设置单价
        $model = new Common();
        $list = $model->query("call checkBookPrice();");
        if(count($list) > 0 && count($list[0]) > 0){
            $return = [
                'status' => false,
                'msg'    => '存在图书未设置单价，请在教材信息管理中先设置好单价',
                'data'   => ''
            ];
            return $return;
        } 

        // 判断是否存在教材没有设置折扣价
        $model = new Common();
        $list = $model->query("call checkBookDisc();");
        if(count($list) > 0 && count($list[0]) > 0){
            $return = [
                'status' => false,
                'msg'    => '存在图书未设置折扣价，请在教材信息管理中先设置好折扣价',
                'data'   => ''
            ];
            return $return;
        } 

        $operid = session('manage')['id'];
         
        $model = new Common();
        $model->query("call bookAccount('".$operid."');");
         
        $return['data']   = [];
        $return['status'] = true;
        $return['msg']    = '汇总账单成功'; 

        return $return;
    }
}